﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using Wuqi.Webdiyer;

using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Web;
using Maticsoft.DBUtility;

namespace SkWeb.FASJ
{
    
    public partial class WellSchemeManage : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            
            try
            {
                
                this.curUseName.Value = Globals.CurrentUserInfo.USE_NAME;
                BDMenuBLL menuBLL = new BDMenuBLL();
                lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);
                
            }
            catch (Exception ex)
            {

            }

            if (!IsPostBack)
            {
                if (Globals.CurrentUserInfo.JSE_ID != 1)
                {
                    //txtSJR.Text = Globals.CurrentUserInfo.USE_NAME;
                    //txtSJR.Enabled = false;
                }
                txtDateStart.Text = DateTime.Now.AddMonths(-2).ToString("yyyy-MM-dd");
                txtDateEnd.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
                SearchData(txtJH.Text.Trim(), txtSJR.Text.Trim(), txtDateStart.Text, txtDateEnd.Text);
            }
        }

        protected void aspNetPagerSearch_PageChanging(object sender, PageChangingEventArgs e)
        {
            aspNetPagerSearch.CurrentPageIndex = e.NewPageIndex;
            SearchData(Request.Form["txtJH"].Trim(), Request.Form["txtSJR"].Trim(),
                    Request.Form["txtDateStart"],
                    Request.Form["txtDateEnd"]);
        }

        private void SearchData(string jh, string shr, string dateStart, string dateEnd)
        {
            try
            {
                int pageIndex = this.aspNetPagerSearch.CurrentPageIndex;
                int pageSize = this.aspNetPagerSearch.PageSize;
                int recordCount = 0;


                DataTable table = null;
                SkgysjbBLL skgyBLL = new SkgysjbBLL();

                table = skgyBLL.QueryWellSchemeDesignByUser(jh, shr, 
                    DateTime.Parse(dateStart).ToString("yyyy-MM-dd"), DateTime.Parse(dateEnd).ToString("yyyy-MM-dd"),
                    pageSize, pageIndex, out recordCount);

                this.gv_wellSchemeDesignList.PageSize = pageSize;

                this.gv_wellSchemeDesignList.DataSource = table;
                this.gv_wellSchemeDesignList.DataBind();
                this.aspNetPagerSearch.RecordCount = recordCount;
                this.aspNetPagerSearch.DataBind();
            }
            catch (Exception ex)
            {

            }


        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            SearchData(Request.Form["txtJH"].Trim(), Request.Form["txtSJR"].Trim(),
                    Request.Form["txtDateStart"],
                    Request.Form["txtDateEnd"]);
        }

    
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow row = btn.NamingContainer as GridViewRow;
            string strJh=row.Cells[0].Text.Trim();
            string strTmp=row.Cells[6].Text.Trim();
            string strSql = "select id from skgysjb where trim(jh)='" + strJh + "'";
            string strJing_id = OracleHelper.Query(strSql).Tables[0].Rows[0][0].ToString();
            
            strSql = "select tmp_id from tb_template_menu where tmp_mc='" + strTmp + "'";
            string strTemplate_id = OracleHelper.Query(strSql).Tables[0].Rows[0][0].ToString();
            strSql = "delete from tb_fa where jing_id=" + strJing_id + "and tmp_id=" + strTemplate_id;
            OracleHelper.ExecuteSql(strSql);

            txtDateStart.Text = DateTime.Now.AddMonths(-2).ToString("yyyy-MM-dd");
            txtDateEnd.Text = DateTime.Now.ToString("yyyy-MM-dd");
            SearchData(txtJH.Text.Trim(), txtSJR.Text.Trim(), txtDateStart.Text, txtDateEnd.Text);

        }

        protected void expExcel_Click(object sender, EventArgs e)
        {


            string strSql = "select well_name 井号, user_name 用户名,action_type 操作类型,time 日期   from user_action where 1=1";
            if ((!string.IsNullOrEmpty(txtDateEnd.Text)) && (!string.IsNullOrEmpty(txtDateStart.Text)))
            {
                strSql += " and time between to_date('" + txtDateStart.Text + "','yyyy-mm-dd') and to_date('" + txtDateEnd.Text + "','yyyy-mm-dd')";
            }


            DataTable dt = OracleHelper.Query(strSql).Tables[0];

            CreateExcel(dt, "log.xls");


        }




        private string[] biaoti = new string[] { "井号", "用户名" ,"操作类型","时间"};

        /// <summary>
        /// 由DataTable导出Excel
        /// </summary>
        /// <param name="dt">需要导出的DATATABLE</param>
        /// <param name="fileName">导出表的名称</param>
        private void CreateExcel(DataTable dt, string fileName)
        {
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            string colHeaders = "", ls_item = "";

            ////定义表对象与行对象，同时用DataSet对其值进行初始化
            //DataTable dt = ds.Tables[0];
          //  DataRow[] myRow = dt.Rows;//Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
            int i = 0;
            int cl = dt.Columns.Count;

            //取得数据表各列标题，各标题之间以t分割，最后一个列标题后加回车符
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列，加n
                {
                    colHeaders += biaoti[i].ToString() + "\n"; //dt.Columns[i].Caption.ToString()
                }
                else
                {
                    colHeaders += biaoti[i].ToString() + "\t"; //dt.Columns[i].Caption.ToString()
                }

            }
            resp.Write(colHeaders);
            //向HTTP输出流中写入取得的数据信息

            //逐行处理数据 
            foreach (DataRow row in dt.Rows)
            {
                //当前行数据写入HTTP输出流，并且置空ls_item以便下行数据   
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))//最后一列，加n
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }

                }
                resp.Write(ls_item);
                ls_item = "";

            }
            resp.End();
        }






    }
}